<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .business-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .img{
   width: 150px;
   height: 100px;
   float: left;
   margin: 5px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>商户列表</span>
    </div>
    <div class="line"></div>
    <business-form
      class="business-form"
      @on-success-valid="handleSubmit">
    </business-form>
    <Button
      icon="md-add"
      type="primary"
      @click="handleClick">
      创建商户
    </Button>
    <business-modal
      @business-valid="handleEdit">
    </business-modal>
    <confirm-modal @confirm-valid="handleConfirm"></confirm-modal>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import BusinessForm from '_c/business-form'
import BusinessModal from '_c/business-modal'
import ConfirmModal from '_c/confirm-modal'
import TablePage from '_c/table-page'
export default {
  components: {
    BusinessForm,
    BusinessModal,
    TablePage,
    ConfirmModal
  },
  data () {
    return {
      page: 1,
      title: '商户管理商户列表',
      index: '',
      confirmData: {},
      playload: null,
      postData: null,
      rows: 20,
      loading: true,
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.tid)
          }
        },
        {
          title: '最近登录时间',
          key: 'logintime',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.logintime ? params.row.logintime : '-')
          }
        },
        {
          title: '注册日期',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '企业名称',
          key: 'company',
          minWidth: 120,
          align: 'center'
        },
        {
          title: '商户姓名',
          key: 'uname',
          minWidth: 120,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.uname ? params.row.uname : '-')
          }
        },
        {
          title: '商户电话',
          key: 'tel',
          minWidth: 115,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.tel ? params.row.tel : '-')
          }
        },
        {
          title: '微信appId',
          key: 'appid',
          minWidth: 160,
          align: 'center'
        },
        {
          title: '短信标签',
          key: 'tag',
          align: 'center',
          minWidth: 120
        },
        {
          title: '操作',
          key: 'cz',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  ghost: 'ghost'
                },
                style: {
                  marginRight: '5px'
                },
                on: {
                  click: () => {
                    this.handleClick(params.row.id, params.row)
                  }
                }
              }, '编辑')
            ])
          }
        }
      ]
    }
  },
  computed: {
    ...mapState({
      businessList: state => state.business.businessList,
      dataCount: state => state.business.businessDataCount,
      historyData: state => state.business.businessHistoryData,
      pageSize: state => state.business.businessPageSize
    })
  },
  methods: {
    ...mapActions([
      'getBusinessList',
      'getBusinessCurrentRow',
      'editBusinessData',
      'updateModalState',
      'changeBusinessPage',
      'changeBusinessPageSize',
      'updateConfirmModalState',
      'getAgentList'
    ]),
    handleSubmit (playload) {
      this.playload = playload
      this.loading = true
      this.getBusinessList(playload).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleClick (id, row) {
      let newRow = {}
      this.id = id
      if (row) {
        newRow = Object.assign({}, row)
      }
      this.getBusinessCurrentRow(newRow)
      this.updateModalState(true)
    },
    handleEdit (playload) {
      this.postData = playload
      this.updateModalState(false)
      this.updateConfirmModalState(true)
    },
    async handleConfirm () {
      this.loading = true
      const resp = await this.editBusinessData(this.postData)
      if (resp.data.state === '000') {
        this.$Message.success('编辑成功')
        this.getBusinessList({
          page: this.page,
          rows: this.rows,
          ...this.playload
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      } else {
        this.$Message.error(resp.data.msg)
      }
      this.updateConfirmModalState(false)
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeBusinessPage(index)
      this.getBusinessList({
        page: index,
        rows: this.rows,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeBusinessPageSize(value)
      this.changePage(this.page)
      this.getBusinessList({
        rows: value,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getBusinessList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
    this.getAgentList({})
  }
}
</script>
